﻿Imports System.IO
Imports System.ComponentModel

Public Class Processor

    Public Enum FileType
        MatchFile
        AplConfirmFile
        TradeSuiteConfirmFile
        AdventConfirmFile
    End Enum

    Public Sub New()
        BackgroundReporter.BackgroundWorker = Nothing 'Switch off reporting
    End Sub

    Public Sub New(ByVal worker As BackgroundWorker)
        BackgroundReporter.BackgroundWorker = worker
    End Sub

    Public Function Start(ByVal fileList() As String, ByVal userID As Integer) As List(Of String)
        Dim archiveIdList As New List(Of String)
        With SystemSettings.Instance
            'Process the match file first if it exists.
            For Each file In fileList
                Dim filePath As String = Path.Combine(SystemSettings.Instance.DataFilePath, file)
                If IsMatchingFile(file, FileType.MatchFile) Then ' Process match files
                    Dim mp As New MatchProcessor
                    mp.Process(filePath)
                End If
            Next

            For Each file In fileList
                Dim filePath As String = Path.Combine(SystemSettings.Instance.DataFilePath, file)
                If IsMatchingFile(file, FileType.AplConfirmFile) Then ' APL Confirm File
                    Dim apl As New AplProcessor
                    archiveIdList.Add(apl.Process(filePath, userID).ToString())
                Else If IsMatchingFile(file, FileType.TradeSuiteConfirmFile) Then ' TradeSuite Confirm File
                    Dim tradeSuite As New TradeSuiteProcessor
                    archiveIdList.Add(tradeSuite.Process(filePath, userID).ToString())
                ElseIf IsMatchingFile(file, FileType.AdventConfirmFile) Then ' Advent Confirm File
                    Dim advent As New AdventProcessor
                    archiveIdList.Add(advent.Process(filePath, userID).ToString())
                End If
            Next

            BackgroundReporter.ReportProgress("Archiving source files...")
            'Now archive the source files
            Dim fileArchiver As New FileArchiver
            fileArchiver.ArchiveConfirmFiles(SystemSettings.Instance.DataFilePath, fileList, Path.Combine(.DataFilePath, "Archive"))
        End With
        BackgroundReporter.ReportProgress(String.Empty, "Archiving complete.")
        Return archiveIdList
        'TODO: Try catch, log and alert user about errors. BackgroundWorker doesn't seem to be parsing and sending information back.
    End Function

    Public Shared Function IsMatchingFile(ByVal sourceFileName As String) As Boolean
        With SystemSettings.Instance
            Return IsMatchingFile(sourceFileName, .MatchFileName) Or IsMatchingFile(sourceFileName, .AplConfirmFileName) Or IsMatchingFile(sourceFileName, .TSConfirmFileName) Or IsMatchingFile(sourceFileName, .AdventConfirmFileName)
        End With
    End Function

    Private Shared Function IsMatchingFile(ByVal sourceFileName As String, ByVal type As FileType) As Boolean
        Dim targetFilePattern As String = String.Empty
        With SystemSettings.Instance
            Select Case type
                Case FileType.MatchFile : targetFilePattern = .MatchFileName
                Case FileType.AplConfirmFile : targetFilePattern = .AplConfirmFileName
                Case FileType.TradeSuiteConfirmFile : targetFilePattern = .TSConfirmFileName
                Case FileType.AdventConfirmFile : targetFilePattern = .AdventConfirmFileName
                Case Else : Throw New Exception("Unknown file type")
            End Select
        End With
        Return IsMatchingFile(sourceFileName, targetFilePattern)
    End Function


    Private Shared Function IsMatchingFile(ByVal sourceFileName As String, ByVal targetFilePattern As String) As Boolean
        Dim isMatch As Boolean = False
        If targetFilePattern <> "" Then
            sourceFileName = sourceFileName.ToUpper()
            targetFilePattern = targetFilePattern.ToUpper()

            isMatch = sourceFileName.StartsWith(targetFilePattern)
        End If

        Return isMatch
    End Function

    Public Shared Function GetArchiveFileSet() As List(Of String)
        Dim archiveList As New List(Of String)
        With SystemSettings.Instance
            For Each file In Directory.GetFiles(.DataFilePath)
                Dim fileName = Path.GetFileName(file)
                If IsMatchingFile(fileName) Then
                    archiveList.Add(fileName)
                End If
            Next
        End With
        Return archiveList
    End Function

End Class
